﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace ProjectNocturnalFool
{
    /// <summary>
    /// Determines the logic behind button clicks
    /// </summary>
    public enum Button_Type
    {
        //Title Menu
        [Description("New")]TitleCampaignNew,
        [Description("Load")]
        TitleCampaignLoad,
        [Description("Arena")]TitleArenaStart,
        [Description("Options")]TitleOptions,
        [Description("Exit")]TitleExitGame,

        //Arena
        [Description("Exit")]ArenaToTitle,
        [Description("Walk")]
        ArenaWalk,
        [Description("End Turn")]
        ArenaEndTurn,

        //Adventure Select
        [Description("Regular Adventure")]
        AdventureSelectRegular,
        [Description("Hard Adventure")]
        AdventureSelectHard,
        [Description("$DLC Adventure$")]
        AdventureSelectDLC,
        [Description("Cancel")]
        AdventureSelectCancel,

        //Party Select
        [Description("Cancel")]
        PartySelectCancel,
        [Description("Load Party")]
        PartySelectLoad,
        [Description("Random Party")]
        PartySelectRandom,
        [Description("Begin")]
        PartySelectBegin,
        [Description("")]
        PartySelectPortrait,

        //Character Portrait
        [Description("Edit")]
        PortraitEdit,
        [Description("Load")]
        PortraitLoad,
        [Description("Random")]
        PortraitRandom,

        
        //Character Edit
        [Description("Cancel")]
        EditBack,
        [Description("Reset")]
        EditReset,
        [Description("Confirm")]
        EditConfirm,
        [Description("Gender")]
        EditGender,
        [Description("Race")]
        EditRace,
        [Description("Class")]
        EditClass,
        [Description("Alignment")]
        EditAlignment,
        [Description("Abilities")]
        EditAbilities,

        //Gender
        [Description("")]
        GenderButton,
        [Description("Cancel")]
        GenderCancel,

        //HACK
        Multi,        
    }

    public enum ClickStatus
    {
        None,
        Clicked
    }

    /// <summary>
    /// Determines button look and feel
    /// </summary>
    public enum Button_Style
    {
        Basic,
        Long,
        Short,
        Image,
        Radio
    }


    class ButtonControl : UserControl
    {
        #region Fields

        public SpriteFont font;
        
        public bool active = true;

        public Color borderColor = Color.Gray;
        public Color selectBorderColor = Color.DarkKhaki;

        public int borderSize = 2;
        
        /// <summary>
        /// For radio style buttons
        /// </summary>
        protected bool pressed = false;

        protected Button_Style _style;
        public Button_Style style
        {
            get { return _style; }
            set
            {
                _style = value;
                switch (value)
                {
                    case Button_Style.Basic:
                        _bgTexture = TacticsGame.Instance.Content.Load<Texture2D>(Constants.TextureButtonRegular);
                        break;
                    case Button_Style.Long:
                        _bgTexture = TacticsGame.Instance.Content.Load<Texture2D>(Constants.TextureButtonRegular);
                        width = Constants.ButtonWidthWide;
                        break;
                        case Button_Style.Short:
                        _bgTexture = TacticsGame.Instance.Content.Load<Texture2D>(Constants.TextureButtonRegular);
                        width = Constants.ButtonWidthShort;
                        break;

                }
            }
        }

        public Button_Type _type;
        public Button_Type type
        {
            get { return _type; }
            set
            {
                _type = value;
                message = type.Description();
            }
        }

        /// <summary>
        /// For buttons, changing the bg texture resizes the entire control
        /// </summary>
        public Texture2D bgTexture
        {
            get { return _bgTexture; }
            set
            {
                _bgTexture = value;
                width = value.Width;
                height = value.Height;
            }
        }

        protected string message;

        #endregion

        /// <summary>
        /// Loads Content of button
        /// </summary>
        public override void LoadContent()
        {
            base.LoadContent();

            _bgTexture = TacticsGame.Instance.Content.Load<Texture2D>(Constants.TextureButtonRegular);
            font = TacticsGame.Instance.gameFont;
            message = "";

            width = Constants.ButtonWidth;
            height = Constants.ButtonHeight;
            depth = ControlDepth.NonDrag;
        }

        /// <summary>
        /// Updates logic based on mouse
        /// </summary>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void HandleClick()
        {
            base.HandleClick();

            if (active)
            {
                if (style != Button_Style.Radio)
                {
                    updateStatus.Add(Tuple.Create(typeof(Button_Type).Name, (int)type, 0));
                }
                else if (style == Button_Style.Radio)
                {
                    pressed = !pressed;
                }
            }
        }
        
        /// <summary>
        /// Draws button based on location
        /// </summary>
        /// <param name="gameTime">gameTime since last draw</param>
        public override void Draw(GameTime gameTime)
        {            
            Rectangle targetRect = new Rectangle(x, y, width, height);

            TacticsGame.Instance.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone);

            Color activeColorText = active ? Color.White : Color.Gray;

            if (style != Button_Style.Radio)
            {
                Color drawBG = style == Button_Style.Image ? Color.White : Color.Black;
           
                    TacticsGame.Instance.spriteBatch.Draw(_bgTexture,
                                                          targetRect, null,
                                                          drawBG,
                                                          0,
                                                          Vector2.Zero,
                                                          SpriteEffects.None,
                                                          0);
            }
            else if (style == Button_Style.Radio)
            {
                    if (pressed)
                    {
                        TacticsGame.Instance.spriteBatch.Draw(_bgTexture,
                                                              targetRect, null,
                                                              Color.Black,
                                                              0,
                                                              Vector2.Zero,
                                                              SpriteEffects.None,
                                                              0);
                    }
                    else
                    {
                        TacticsGame.Instance.spriteBatch.Draw(_bgTexture,
                                                              targetRect, null,
                                                              Color.Black,
                                                              0,
                                                              Vector2.Zero,
                                                              SpriteEffects.None,
                                                              0);
                    }
            }

            Vector2 size = font.MeasureString(message);
            int xPos = Util.getCentreDrawPointX(x, width, (int)size.X);

            int yPos = Util.getCentreDrawPointY(y, height, (int)size.Y);

                TacticsGame.Instance.spriteBatch.DrawString(font,
                                                            message,
                                                            new Vector2(xPos, yPos),
                                                            activeColorText,
                                                            0.0f,
                                                            new Vector2(0, 0),
                                                            Constants.FontScale,
                                                            SpriteEffects.None,
                                                            0);

                if (Hover && active)
                    Util.drawBorder(targetRect, selectBorderColor, borderSize);
                else
                    Util.drawBorder(targetRect, borderColor, borderSize);


            TacticsGame.Instance.spriteBatch.End();
            
        }

        /// <summary>
        /// Constructs button object
        /// </summary>
        public ButtonControl()
            : base()
        {

        }
    }
}
